﻿using System.Net.NetworkInformation;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;

namespace ZhihuiApp;

public partial class MainForm : Form
{
    private readonly WebTool _tool;
    private readonly string _url;
    private readonly WebView2 _webView;

    public MainForm()
    {
        _webView = new WebView2();

        _tool = new WebTool(ref _webView, this);

        _url = ReadConfig();

        InitializeComponent();
    }

    /// <summary>
    ///     关闭对象化进行二次确认
    /// </summary>
    /// <param name="disposing"></param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && components != null) components.Dispose();

        base.Dispose(disposing);
    }


    /// <summary>
    ///     暴露一个变量费JS调用
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ExportObjects(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
        //webview和js交互
        var macAddress = NetworkInterface.GetAllNetworkInterfaces()
            .Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
            .Where(nic => nic.OperationalStatus == OperationalStatus.Up)
            .Select(nic => nic.GetPhysicalAddress().ToString()).FirstOrDefault();
        _webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync($"var macAddress='{macAddress}'");

        //  暴露一个包含JS交互的对象工具到浏览器
        _webView.CoreWebView2.AddHostObjectToScript("tool", _tool);


        //开启开发者工具
        //    webView.CoreWebView2.OpenDevToolsWindow();
        //禁止dev菜单
        _webView.CoreWebView2.Settings.AreDevToolsEnabled = true;
        //禁止所有菜单
        _webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
    }


    /// <summary>
    ///     重写关闭按钮 进行二次确认
    /// </summary>
    /// <param name="msg"></param>
    protected override void WndProc(ref Message msg)
    {
        const int wmSubcommand = 0x0112;
        const int scClose = 0xF060;
        if (msg.Msg == wmSubcommand && (int)msg.WParam == scClose)
        {
            // 点击winform右上关闭按钮 
            // 加入想要的逻辑处理
            const MessageBoxButtons mess = MessageBoxButtons.OKCancel;
            var dr = MessageBox.Show(@"确定要退出系统吗？", @"提示", mess);
            if (dr == DialogResult.OK) Application.Exit();
            return;
        }

        base.WndProc(ref msg);
    }

    //改变窗口大小时webview2跟着改变
    protected void Form_Resize(object sender, EventArgs e)
    {
        _webView.Dock = DockStyle.Fill;
    }


    /// <summary>
    ///     读取配置参数
    /// </summary>
    /// <returns></returns>
    private   string ReadConfig()
    {
        try
        {
            var filePath = "./resource/config.ini"; // 指定要读取的JSON文件路径
            var fileContent = File.ReadAllText(filePath);
            return fileContent;
        }
        catch (Exception e)
        {
            Console.WriteLine(@"文件读取失败"+e);
         
        }
        return "https://www.baidu.com";
    }


    /// <summary>
    ///     修改主程序程序信息
    /// </summary>
    /// <param name="text"></param>
    public void OnChangeText(string text)
    {
        Console.WriteLine(@"修改主程序");
        Text = @"修改后的标题";
        // 关闭程序
        //  Application.Exit();
    }
    
    
    
    
}